package com.winchain.chainbill.bill.base.service.impl;

import com.shell.bee.persistence.service.impl.PersistentServiceImpl;
import com.winchain.chainbill.bill.base.dao.CbRecDao;
import com.winchain.chainbill.bill.base.dao.CbXfeDao;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.base.po.CbXfe;
import com.winchain.chainbill.bill.base.service.CbXfeService;
import com.winchain.chainbill.bill.biz.transfer.vo.BillTransfer;
import com.winchain.chainbill.bill.biz.transfer.vo.BillXfeRecVo;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("cbXfeService")
public class CbXfeServiceImpl extends PersistentServiceImpl<CbXfe> implements CbXfeService {
	@Autowired
	private CbXfeDao cbXfeDao;
	@Autowired
	private CbRecDao cbRecDao;

	public List<BillTransfer> selectBillXfesByBillIdAndState(String prntId, String xfeState) {
		List<BillTransfer> bills = this.cbXfeDao.selectBillXfesByPrntIdAndBillState(prntId, "S000");
		if ((bills == null) || (bills.isEmpty())) {
			bills = this.cbXfeDao.selectBillXfesByBillIdAndState(prntId, xfeState);
			if ((bills != null) && (bills.size() > 0)) {
				((BillTransfer) bills.get(0)).setXfePrntId(prntId);
			}
		} else {
			CbRec cbRec = (CbRec) this.cbRecDao.selectByPrimaryKey(prntId);
			BillTransfer billTransfer = new BillTransfer();
			billTransfer.setBillId(cbRec.getBillId());
			billTransfer.setBillCode(cbRec.getBillCode());
			billTransfer.setCbOptSeq(cbRec.getOptSeq());
			billTransfer.setBillType(cbRec.getBillType());
			billTransfer.setRootId(cbRec.getRootId());
			billTransfer.setPrntId(cbRec.getPrntId());
			billTransfer.setBillAmt(cbRec.getBillAmt());
			billTransfer.setHldrLvl(cbRec.getHldrLvl());
			billTransfer.setSndDate(cbRec.getSndDate());
			billTransfer.setBillState(cbRec.getBillState());
			billTransfer.setExpDate(cbRec.getExpDate());
			bills.add(billTransfer);
		}
		return bills;
	}

	public int updateCbXfeState(CbXfe cbXfe) {
		return this.cbXfeDao.updateCbXfeState(cbXfe);
	}

	public List<BillTransfer> selectSubXfesBillByPrntId(String prntId) {
		return this.cbXfeDao.selectSubXfesBillByPrntId(prntId);
	}

	public BillXfeRecVo getBillXfeRecById(String billId) {
		return this.cbXfeDao.getBillSndById(billId);
	}

	public BigDecimal sumXfeAmt(String cusId, String issuer, String strtDate, String... grpIds) {
		return this.cbXfeDao.sumXfeAmt(cusId, issuer, strtDate, grpIds);
	}
}
